package com.atguigu.tingshu.album.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONObject;
import com.atguigu.tingshu.album.mapper.BaseCategory1Mapper;
import com.atguigu.tingshu.album.mapper.BaseCategory2Mapper;
import com.atguigu.tingshu.album.mapper.BaseCategory3Mapper;
import com.atguigu.tingshu.album.mapper.BaseCategoryViewMapper;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.common.config.cache.GuiGuCache;
import com.atguigu.tingshu.model.album.BaseCategory1;
import com.atguigu.tingshu.model.album.BaseCategory2;
import com.atguigu.tingshu.model.album.BaseCategory3;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;

	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;

	/**
	 * 查询所有分类
	 *
	 * @return
	 */
	@Override
	@GuiGuCache(prefix = "baseCategoryList:")
	public List<JSONObject> getBaseCategorylist() {
		//查询一级分类
		ArrayList<JSONObject> allList = new ArrayList<>();
		List<BaseCategoryView> categoryList1 = baseCategoryViewMapper.selectList(null);
		//获取一级分类
		Map<Long, List<BaseCategoryView>> list = categoryList1.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
		if (CollectionUtil.isNotEmpty(list)) {
			for (Map.Entry<Long, List<BaseCategoryView>> entry : list.entrySet()) {
				JSONObject json1 = new JSONObject();
				Long categoryId = entry.getKey();
				String categoryName = entry.getValue().get(0).getCategory1Name();
				json1.put("categoryId", categoryId);
				json1.put("categoryName", categoryName);
				Map<Long, List<BaseCategoryView>> list2 = entry.getValue().stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
				if (CollectionUtil.isNotEmpty(list2)) {
					ArrayList<JSONObject> jsonObjects2 = new ArrayList<>();
					for (Map.Entry<Long, List<BaseCategoryView>> entry2 : list2.entrySet()) {
						Long category2Id = entry2.getKey();
						String category2Name = entry2.getValue().get(0).getCategory2Name();
						JSONObject json2 = new JSONObject();
						json2.put("categoryId", category2Id);
						json2.put("categoryName", category2Name);
						Map<Long, List<BaseCategoryView>> list3 = entry2.getValue().stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory3Id));
						if (CollectionUtil.isNotEmpty(list3)) {
							ArrayList<JSONObject> jsonObjects3 = new ArrayList<>();
							for (Map.Entry<Long, List<BaseCategoryView>> entry3 : list3.entrySet()) {
								JSONObject json3 = new JSONObject();
								Long category3Id = entry3.getKey();
								String category3Name = entry3.getValue().get(0).getCategory3Name();
								json3.put("categoryId", category3Id);
								json3.put("categoryName", category3Name);
								jsonObjects3.add(json3);
							}
							json2.put("categoryChild", jsonObjects3);
						}
						jsonObjects2.add(json2);
					}
					json1.put("categoryChild", jsonObjects2);
				}
				allList.add(json1);
			}
		}
		return allList;
	}

	@Override
	@GuiGuCache(prefix = "category3View:")
	public BaseCategoryView getCategoryView(Long category3Id) {
		return baseCategoryViewMapper.selectById(category3Id);
	}

	@Override
	@GuiGuCache(prefix = "baseCategory3Top7:")
	public List<BaseCategory3> getTop7BaseCategory3(Long category1Id) {
		//查找二级分类
		LambdaQueryWrapper<BaseCategory2> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(BaseCategory2::getCategory1Id,category1Id);
		wrapper.select(BaseCategory2::getId);

		List<BaseCategory2> baseCategory2List = baseCategory2Mapper.selectList(wrapper);

        if (CollectionUtil.isNotEmpty(baseCategory2List)) {
			Set<Long> list = baseCategory2List.stream().map(basecategory2 -> basecategory2.getId()).collect(Collectors.toSet());
			LambdaQueryWrapper<BaseCategory3> lambdaQueryWrapper = new LambdaQueryWrapper<>();
			lambdaQueryWrapper.in(BaseCategory3::getCategory2Id,list);
			lambdaQueryWrapper.eq(BaseCategory3::getIsTop,1);
			lambdaQueryWrapper.orderByDesc(BaseCategory3::getOrderNum);
			lambdaQueryWrapper.last("limit 7");
			return baseCategory3Mapper.selectList(lambdaQueryWrapper);
		}
		return null;
	}

	@Override
	@GuiGuCache(prefix = "baseCategoryListByCategory1Id:")
	public JSONObject getBaseCategoryListByCategory1Id(Long category1Id) {
		LambdaQueryWrapper<BaseCategoryView> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(BaseCategoryView::getCategory1Id,category1Id);

		List<BaseCategoryView> baseCategory1List = baseCategoryViewMapper.selectList(queryWrapper);
        if (CollectionUtil.isNotEmpty(baseCategory1List)) {
			JSONObject jsonObject1 = new JSONObject();
			jsonObject1.put("categoryId",baseCategory1List.get(0).getCategory1Id());
			jsonObject1.put("categoryName",baseCategory1List.get(0).getCategory1Name());

			Map<Long, List<BaseCategoryView>> category2Map = baseCategory1List.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
			//3.2 遍历Map每遍历一次Map封装二级分类JSON对象
			List<JSONObject> jsonObject2List = new ArrayList<>();
			for (Map.Entry<Long, List<BaseCategoryView>> entry2 : category2Map.entrySet()) {
				//3.3 构建二级分类对象，封装二级分类ID及名称
				JSONObject jsonObject2 = new JSONObject();
				jsonObject2.put("categoryId", entry2.getKey());
				jsonObject2.put("categoryName", entry2.getValue().get(0).getCategory2Name());
				jsonObject2List.add(jsonObject2);
				//4.处理二级分类下三级分类
				//4.1 遍历二级分类列表，没遍历一条记录构建三级分类对象
				List<JSONObject> jsonObject3List = new ArrayList<>();
				for (BaseCategoryView baseCategoryView : entry2.getValue()) {
					//4.2 构建三级分类对象，封装三级分类ID名称
					JSONObject jsonObject3 = new JSONObject();
					jsonObject3.put("categoryId", baseCategoryView.getCategory3Id());
					jsonObject3.put("categoryName", baseCategoryView.getCategory3Name());
					jsonObject3List.add(jsonObject3);
				}
				//4.3 将三级分类集合放入二级分类对象中
				jsonObject2.put("categoryChild", jsonObject3List);
			}
			//3.3 将二级分类集合放入一级分类对象中
			jsonObject1.put("categoryChild", jsonObject2List);
			return jsonObject1;
		}
		return null;
	}
}
